package com.cyanite.playlv2.widget;

import com.cyanite.playlv2.R;
import com.cyanite.playlv2.util.RemoteResourceManager;
import com.cyanite.playlv2api.types.Task;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.ToggleButton;

import java.io.IOException;
import java.util.Observable;
import java.util.Observer;

public class FocusSubTripListAdapter extends BaseTaskAdapter implements ObservableAdapter {

    private LayoutInflater mInflater;
    private RemoteResourceManager mRrm;
    private RemoteResourceManagerObserver1 mResourcesObserver1;
    private RemoteResourceManagerObserver2 mResourcesObserver2;
    private Handler mHandler1 = new Handler();
    private Handler mHandler2 = new Handler();
    private ButtonRowClickHandler mClickListener;
    private boolean mMoreRefresh;
    private boolean mMoreShow;
    private Context mContext;

    public FocusSubTripListAdapter(Context context, RemoteResourceManager rrm, ButtonRowClickHandler clickListener, boolean moreShow) {
        super(context);
        mInflater = LayoutInflater.from(context);
        mRrm = rrm;
        mResourcesObserver1 = new RemoteResourceManagerObserver1();
        mRrm.addObserver(mResourcesObserver1);
        mResourcesObserver2 = new RemoteResourceManagerObserver2();
        mRrm.addObserver(mResourcesObserver2);
        mClickListener = clickListener;
        mMoreRefresh = false;
        mMoreShow = moreShow;
        mContext = context;
    }

    public void removeObserver() {
        mHandler1.removeCallbacks(mUpdateUserPhoto);
        mHandler1.removeCallbacks(mUpdateTaskPhoto);
        mRrm.deleteObserver(mResourcesObserver1);
        mRrm.deleteObserver(mResourcesObserver2);
    }

    public void setMoreRefresh(boolean isRefresh)    {
        mMoreRefresh = isRefresh;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.trip_list_focus_showing, null);
            holder = new ViewHolder();
            holder.userPhoto = (ImageView) convertView.findViewById(R.id.imgUserIcon);
            holder.userName = (TextView) convertView.findViewById(R.id.tvFocusUserName);
            holder.desc1 = (TextView) convertView.findViewById(R.id.tvShareDescription1);
            holder.desc2 = (TextView) convertView.findViewById(R.id.tvShareDescription2);
            holder.desc3 = (TextView) convertView.findViewById(R.id.tvShareDescription3);
            holder.desc4 = (TextView) convertView.findViewById(R.id.tvShareDescription4);
            holder.textShout = (TextView) convertView.findViewById(R.id.tvFocusTripShout);
            holder.textAddress = (TextView) convertView.findViewById(R.id.tvFocusTripAddress);
            holder.photo = (ImageView) convertView.findViewById(R.id.imgTripFocusPhoto);
            holder.postDate = (TextView) convertView.findViewById(R.id.tvFocusPostDate );
            holder.tipsCount = (TextView) convertView.findViewById(R.id.tvFocusTips);
            holder.tgBtnFoldingIndicator = (ToggleButton) convertView.findViewById(R.id.imgFoldingIndicatorOfFocusTripList);
            holder.more = (LinearLayout) convertView.findViewById(R.id.llMore);
            holder.mProgressBarMore = (ProgressBar) convertView.findViewById(R.id.pbMore);
            if (mMoreShow == true) {
                holder.more.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        holder.mProgressBarMore.setVisibility(View.VISIBLE);
                        mClickListener.onMoreClick();
                        mMoreRefresh = true;
                    }
                });
            }
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        int count = 0;
        if (mMoreShow) {
            count = getCount() - 1;
        } else {
            count = getCount();
        }
        if  (position != count)  {
            holder.more.setVisibility(View.GONE);
            holder.userPhoto.setVisibility(View.VISIBLE);
            holder.userName.setVisibility(View.VISIBLE);
            holder.textShout.setVisibility(View.GONE);
            holder.textAddress.setVisibility(View.VISIBLE);
            holder.photo.setVisibility(View.VISIBLE);
            holder.postDate.setVisibility(View.VISIBLE);
            holder.tipsCount.setVisibility(View.VISIBLE);
            holder.tgBtnFoldingIndicator.setVisibility(View.VISIBLE);

            Task task = (Task) getItem(position);
            if (task.getUserPhoto() != null)  {
                Uri photoUri = Uri.parse(task.getUserPhoto());
                if (!mRrm.exists(photoUri)) {
                    mRrm.request(photoUri);
                }
            }
            if (task.getPhoto() != null)  {
                Uri photoUri = Uri.parse(task.getPhoto());
                if (!mRrm.exists(photoUri)) {
                    mRrm.request(photoUri);
                }
            }
            convertView.setPadding(0, 3, 7, 10);
            holder.userName.setText(task.getUserName());

            String date = task.getPostDate();

            date+="      ";
            String origin;
            switch (task.getAccessway()) {
                case 1:
                    origin=mContext.getString(R.string.share_access_way_network);
                    break;
                case 2:
                    origin=mContext.getString(R.string.share_access_way_android);
                    break;
                default:
                    origin=mContext.getString(R.string.share_access_way_null);
                    break;
            }
            holder.postDate.setText(date+origin);

            if (task.getVenueName() != null)    {
                String address= "@";
                address += task.getVenueName();
                holder.textAddress.setText(address);
            }

            switch(task.getInfo().getInfoCount()){
                case 1:
                    if ( task.getInfo().getInfo1() != null ){
                        holder.textShout.setText(task.getInfo().getInfo1());
                        holder.textShout.setVisibility(View.VISIBLE);
                    }
                    break;
                case 3:
                    if(task.getInfo().getInfo3()!=null){
                        holder.desc1.setVisibility(View.VISIBLE);
                        holder.desc2.setVisibility(View.VISIBLE);
                        holder.desc1.setText(task.getInfo().getInfo1());
                        holder.desc2.setText(task.getInfo().getInfo2());
                        holder.textShout.setText(task.getInfo().getInfo3());
                        holder.textShout.setVisibility(View.VISIBLE);
                    }
                    break;
                case 4:
                    if (task.getInfo().getInfo4() != null ){
                        holder.desc1.setVisibility(View.VISIBLE);
                        holder.desc2.setVisibility(View.VISIBLE);
                        holder.desc3.setVisibility(View.VISIBLE);
                        holder.desc1.setText(task.getInfo().getInfo1());
                        holder.desc2.setText(task.getInfo().getInfo2());
                        holder.desc3.setText(task.getInfo().getInfo3());
                        holder.textShout.setText(task.getInfo().getInfo4());
                        holder.textShout.setVisibility(View.VISIBLE);
                    }
                    break;
                case 5:
                    if ( task.getInfo().getInfo5() != null ){
                        holder.desc1.setVisibility(View.VISIBLE);
                        holder.desc2.setVisibility(View.VISIBLE);
                        holder.desc3.setVisibility(View.VISIBLE);
                        holder.desc4.setVisibility(View.VISIBLE);
                        holder.desc1.setText(task.getInfo().getInfo1());
                        holder.desc2.setText(task.getInfo().getInfo2());
                        holder.desc3.setText(task.getInfo().getInfo3());
                        holder.desc4.setText(task.getInfo().getInfo4());
                        holder.textShout.setText(task.getInfo().getInfo5());
                        holder.textShout.setVisibility(View.VISIBLE);
                    }
                    break;
            }


            if (task.getTipsCount() > 0)    {
                if (task.getTipsCount() < 10 )  {
                    holder.tipsCount.setText(String.format("  %d", task.getTipsCount()));
                } else {
                    holder.tipsCount.setText(String.valueOf(task.getTipsCount()));
                }
            }
            else holder.tipsCount.setText("  +");

            holder.photo.setVisibility(View.GONE);
            holder.tgBtnFoldingIndicator.setVisibility(View.GONE);

            if (task.getUserPhoto() != null)    {
                final Uri photoUri = Uri.parse(task.getUserPhoto());
                try {
                    Bitmap bitmap = BitmapFactory.decodeStream(mRrm.getInputStream(photoUri));
                    holder.userPhoto.setImageBitmap(bitmap);
                } catch (IOException e) {
                    holder.userPhoto.setImageResource(R.drawable.blank_girl);
                }
            }   else {
                holder.userPhoto.setImageResource(R.drawable.blank_girl);
            }
            if (task.getPhoto() != null)    {
                final Uri photoUri = Uri.parse(task.getPhoto());
                try {
                    Bitmap bitmap = BitmapFactory.decodeStream(mRrm.getInputStream(photoUri));
                    holder.photo.setImageBitmap(bitmap);
                    task.setPhotoBitmap(bitmap);
                    holder.tgBtnFoldingIndicator.setVisibility(View.VISIBLE);
                    holder.photo.setOnClickListener(mOnClickListenerTripImage);
                    holder.photo.setTag(position);

                    holder.tgBtnFoldingIndicator.setBackgroundResource(R.drawable.arrow_down);
                    holder.tgBtnFoldingIndicator.setOnClickListener(new Button.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if  ( holder.tgBtnFoldingIndicator.isChecked() ) {
                                holder.tgBtnFoldingIndicator.setBackgroundResource(R.drawable.arrow_up);

                                holder.photo.setVisibility(View.VISIBLE);
                            } else {
                                holder.tgBtnFoldingIndicator.setBackgroundResource(R.drawable.arrow_down);
                                holder.photo.setVisibility(View.GONE);
                            }
                        }
                    });
                } catch (IOException e) {
                }
            }
        } else if (mMoreShow){
            holder.more.setVisibility(View.VISIBLE);
            holder.userPhoto.setVisibility(View.GONE);
            holder.userName.setVisibility(View.GONE);
            holder.textShout.setVisibility(View.GONE);
            holder.textAddress.setVisibility(View.GONE);
            holder.photo.setVisibility(View.GONE);
            holder.postDate.setVisibility(View.GONE);
            holder.tipsCount.setVisibility(View.GONE);
            holder.tgBtnFoldingIndicator.setVisibility(View.GONE);
        }
        if (!mMoreRefresh)  {
            holder.mProgressBarMore.setVisibility(View.GONE);
        }
        return convertView;
    }

    private class RemoteResourceManagerObserver1 implements Observer {
        @Override
        public void update(Observable observable, Object data) {
            mHandler1.post(mUpdateUserPhoto);
        }
    }

    private class RemoteResourceManagerObserver2 implements Observer {
        @Override
        public void update(Observable observable, Object data) {
            mHandler2.post(mUpdateTaskPhoto);
        }
    }

    private Runnable mUpdateUserPhoto = new Runnable() {
        @Override
        public void run() {
            notifyDataSetChanged();
        }
    };

    private Runnable mUpdateTaskPhoto = new Runnable() {
        @Override
        public void run() {
            notifyDataSetChanged();
        }
    };

    private OnClickListener mOnClickListenerTripImage = new OnClickListener() {
        @Override
        public void onClick(View v) {
            Integer position = (Integer) v.getTag();
            mClickListener.onImageClick((Task) getItem(position));
        }
    };

    public interface ButtonRowClickHandler {
        public void onImageClick(Task task);
        public void onMoreClick();
    }

    private static class ViewHolder {
        ImageView userPhoto;
        TextView userName;
        TextView desc1;
        TextView desc2;
        TextView desc3;
        TextView desc4;
        TextView textShout;
        TextView textAddress;
        ImageView photo;
        TextView postDate;
        TextView tipsCount;
        ToggleButton tgBtnFoldingIndicator;
        LinearLayout more;
        ProgressBar mProgressBarMore;
    }
}
